package com.olap.starter.service;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.olap.starter.core.base.BaseService;
import com.olap.starter.mapper.FormMainCreateJsonMapper;
import com.olap.starter.pojo.FormMain;
import com.olap.starter.pojo.FormMainCreateJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 服务类
 * </p>
 *
 * @author WBG
 * @since 2022-12-28
 */
@Service
@DS("online")
public class FormMainCreateJsonService extends BaseService<FormMainCreateJsonMapper, FormMainCreateJson> {

    @Autowired
    private FormMainServices formMainServices;
    @Autowired
    private FormMainColumnServices formMainColumnServices;

  /*  @Override
    @Transactional
    public boolean save(FormMainCreateJson entity) {
        FormMain formMain = formMainServices.getById(entity.getId());
        formMainServices.update(new LambdaUpdateWrapper<FormMain>().set(FormMain::getDeleted, "1").eq(FormMain::getId, entity.getId()));
        formMain.setVersion(formMain.getVersion() + 1);
        formMain.setId(null);
        formMain.setDesigner("1");
        formMainServices.save(formMain);
        entity.setId(formMain.getId());
        return super.save(entity);
    }*/

    public Map<String, Object> getJson(Integer id, Long uuid, String theme) {
        Map<String, Object> map = new HashMap<>();
        if (id == null) {
            FormMain formMain = formMainServices.getOne(new QueryWrapper<FormMain>()
                    .eq("uuid", uuid)
                    .eq("theme", theme)
                    .eq("deleted", 0));
            map.put("formMain", formMain);
            if (formMain.getDesigner().equals("0")) {
                map.put("formMainColumn", formMainColumnServices.getListByUUid(uuid));
            } else {
                map.put("formMainCreateJson", getById(formMain.getId()));
            }
        } else {
            map.put("formMainCreateJson", getById(id));
            FormMain formMain = formMainServices.getById(id);
            map.put("formMain", formMain);
        }
        return map;
    }
}
